package com.da.javatest.interview.session3;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author JokerDa.
 * @date 2020/12/26 22:35
 * Description: O(n)复杂度的解法
 */
public class Test02TwoSum {
    public int[] twoSum(int[] numbers, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {
            int left = target - numbers[i];
            if (map.get(left) != null) {
                return new int[] {map.get(left), i};
            }
            map.put(numbers[i], i);
        }
        return null;
    }

    public static void main(String[] args) {
        Test02TwoSum twoSum = new Test02TwoSum();
        System.out.println(Arrays.toString(twoSum.twoSum(new int[] {1, 2, 3, 7, 11, 13, 18}, 15)));
    }
}
